package com.ruoyi.eqmonitor.phase1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eqmonitor.dtos.OptDeviceDto;
import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1Service;

/**
 * 一期设备管理
 * @author ruoyi
 * @date 2023-03-17
 */
@RestController
@RequestMapping("/device/phase1")
public class OptDevicePhase1Controller extends BaseController{
    
    @Autowired
    private IOptDevicePhase1Service optDevicePhase1Service;

    @GetMapping("/list")
    public TableDataInfo<?> list(OptDevicePhase1 optDevicePhase1){
        startPage();
        List<OptDevicePhase1> list = optDevicePhase1Service.selectOptDevicePhase1List(optDevicePhase1);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optDevicePhase1Service.selectOptDevicePhase1ById(id));
    }

    @Log(title = "一期设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptDevicePhase1 optDevicePhase1){
    	optDevicePhase1.setCreateTime(new Date());
    	optDevicePhase1.setCreateBy(SecurityUtils.getUsername());
        return toAjax(optDevicePhase1Service.save(optDevicePhase1));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody OptDevicePhase1 optDevicePhase1){
    	optDevicePhase1.setUpdateTime(new Date());
    	optDevicePhase1.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optDevicePhase1Service.updateById(optDevicePhase1));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "一期设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optDevicePhase1Service.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出一期设备管理列表
     */
    @Log(title = "一期设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptDevicePhase1 optDevicePhase1){
        List<OptDevicePhase1> list = optDevicePhase1Service.selectOptDevicePhase1List(optDevicePhase1);
        ExcelUtil<OptDevicePhase1> util = new ExcelUtil<OptDevicePhase1>(OptDevicePhase1.class);
        util.exportExcel(response, list, "一期设备管理数据");
    }
    
    @GetMapping("/listAndLastTime")
    public TableDataInfo<?> listAndLastTime(OptDeviceDto optDevice){
        startPage();
        List<OptDeviceDto> list = optDevicePhase1Service.selectOptDeviceListAndLastTime(optDevice);;
        return getDataTable(list);
    }

    @GetMapping("/listAndMonitorData")
    public AjaxResult listAndMonitorData(OptDevicePhase1 optDevicePhase1){
        return AjaxResult.success(optDevicePhase1Service.listAndMonitorData(optDevicePhase1));
    }
    
}
